package com.pricetag.pricetagandroid;

import java.util.ArrayList;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.pricetag.helpers.Constants;
import com.pricetag.helpers.GPSLocation;
import com.pricetag.helpers.LocationHolder;

import android.location.Location;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MapShowActivity extends FragmentActivity {

	private GoogleMap googleMap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map_show);
		ArrayList<LocationHolder> locationHolder=new ArrayList<LocationHolder>();
		// getting data from intent
		Bundle bundle = this.getIntent().getExtras();
		locationHolder = (ArrayList<LocationHolder>)bundle.get(Constants.LOCATION_STORE);
		

		
		//
		FragmentManager fragmentManager = getSupportFragmentManager();
		SupportMapFragment supportMapFragment = (SupportMapFragment) fragmentManager.findFragmentById(R.id.map);
		googleMap = supportMapFragment.getMap();

		// data is to be taken from intent by following code
	//	Intent intent = getIntent();
		// intent.getBundleExtra(name)
		// String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
		
		// get the current location
		GPSLocation gpsLoc=new GPSLocation(this);
		
		Location currentLocation=gpsLoc.getCurrentLocation();
		
		if(currentLocation != null){
			LatLng latLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());	
		//	googleMap.addMarker(new MarkerOptions().position(latLng).title("Current Location").snippet("Current location of user"));
			googleMap.addMarker(new MarkerOptions().position(latLng).title("Current Location").snippet("Current location of user").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
			googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 8));
		}
		
		else {
			Log.i("Current Location", "It is nulll");
		}
		
		
	//	LatLng latLng = new LatLng(49.2630427, -123.2511662); // current
																// location will
																// be sent by
																// invoker of
																// this api
		//LatLng latLng2 = new LatLng(49.2218477, -123.2319117); // current
																// location will
																// be sent by
																// invoker of
																// this api
		for(int i=0;i<locationHolder.size();i++){
			//LatLng latLng = new LatLng(locationHolder.get(i).getLatitude(), locationHolder.get(i).getLongitude());	
			googleMap.addMarker(new MarkerOptions().position(new LatLng(locationHolder.get(i).getLatitude(), locationHolder.get(i).getLongitude())).title(locationHolder.get(i).getLocationName()));
			Log.i("size",locationHolder.size()+"");
		}
		
		
		
		//googleMap.addMarker(new MarkerOptions().position(latLng).title("Current Location").snippet("This is the description"));
		//googleMap.addMarker(new MarkerOptions().position(latLng2).title("Book Store"));
		//googleMap.addMarker(new MarkerOptions().position(new LatLng(49.326309,-123.135260)).title("Book Store1"));
		//googleMap.addMarker(new MarkerOptions().position(new LatLng(49.211720,-123.106606)).title("Book Store2"));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_map_show, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Log.i("Chee", "this" + item.getItemId());
		if (item.getItemId() == R.id.legal_info) {

			String openSourceLicense = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(getApplicationContext());
			AlertDialog.Builder licenseWindow = new AlertDialog.Builder(MapShowActivity.this);
			licenseWindow.setTitle("Legal Notice");
			Log.i("Chee", "New Working");
			licenseWindow.setMessage(openSourceLicense);
			licenseWindow.show();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

		if (resultCode == ConnectionResult.SUCCESS) {

			Log.i("GoogleMap", "Google Play Services  available");

		} else {

			Log.e("GoogleMap", "Google Play Services not available");
		}

	}

}
